package com.ht.medicaltreatment.viewmodel

import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.api.ConsumableApprovalAPI
import com.ht.medicaltreatment.api.ConsumableRecordAPI
import com.ht.medicaltreatment.bean.ApplyRecordData
import com.ht.medicaltreatment.bean.ConsumableApplyRecordBean

class ConsumableRecordViewModel : BaseViewModel() {
    fun requestData(page:Int,projectId :Int,status :String,onSuccess:(ApplyRecordData)->Unit){
        EasyHttp.post(this)
            .api(ConsumableRecordAPI(page,projectId = projectId, status = status))
            .request(object : OnHttpListener<ConsumableApplyRecordBean>{
                override fun onHttpSuccess(result: ConsumableApplyRecordBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"请求失败")
                }
            })
    }

    fun approval(sign:Int,remark :String = "",id :Int,onSuccess:(ApplyRecordData)->Unit){
        val api = ConsumableApprovalAPI(sign = sign, remark = remark, id = id)
        EasyHttp.post(this)
            .api(api)
            .request(object : OnHttpListener<ConsumableApplyRecordBean>{
                override fun onHttpSuccess(result: ConsumableApplyRecordBean) {
                    if(result.code == 200){
                        onSuccess(result.data)
                    }else{
                        dismissProgress(result.message)
                    }
                }

                override fun onHttpFail(e: Exception) {
                    dismissProgress(e.message?:"请求失败")
                }
            })
    }
}
